<?php

namespace app\controller;

use think\Request;
use app\service\StatisticsService;
use Exception;

class Statistics
{
    protected $statisticsService;

    public function __construct()
    {
        $this->statisticsService = new StatisticsService();
    }

    /**
     * 获取完整统计数据
     */
    public function index(Request $request)
    {
        try {
            $userId = $request->userId;
            $days = $request->param('days', 30);

            $stats = $this->statisticsService->getFullStatistics($userId, $days);

            return success($stats, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取账号使用频率
     */
    public function accountUsage(Request $request)
    {
        try {
            $userId = $request->userId;
            $days = $request->param('days', 30);

            $stats = $this->statisticsService->getAccountUsageStats($userId, $days);

            return success($stats, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取最常使用的账号
     */
    public function mostUsed(Request $request)
    {
        try {
            $userId = $request->userId;
            $limit = $request->param('limit', 10);
            $days = $request->param('days', 30);

            $stats = $this->statisticsService->getMostUsedAccounts($userId, $limit, $days);

            return success($stats, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取使用趋势
     */
    public function trend(Request $request)
    {
        try {
            $userId = $request->userId;
            $days = $request->param('days', 30);

            $trend = $this->statisticsService->getUsageTrend($userId, $days);

            return success($trend, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取分类使用统计
     */
    public function categoryUsage(Request $request)
    {
        try {
            $userId = $request->userId;
            $days = $request->param('days', 30);

            $stats = $this->statisticsService->getCategoryUsageStats($userId, $days);

            return success($stats, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取操作类型统计
     */
    public function actionTypes(Request $request)
    {
        try {
            $userId = $request->userId;
            $days = $request->param('days', 30);

            $stats = $this->statisticsService->getActionTypeStats($userId, $days);

            return success($stats, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取最近活动
     */
    public function recentActivities(Request $request)
    {
        try {
            $userId = $request->userId;
            $limit = $request->param('limit', 20);

            $activities = $this->statisticsService->getRecentActivities($userId, $limit);

            return success($activities, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 获取账号健康度评分
     */
    public function healthScore(Request $request)
    {
        try {
            $userId = $request->userId;

            $score = $this->statisticsService->getAccountHealthScore($userId);

            return success($score, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }
}
